package ch02.LL_1_Parser;

import ch02.LL_1_Lexer.Lexer;
import ch02.LL_1_Lexer.Token;

public class Parser {
    Lexer input;            // 输入的词法单元流
    Token lookahead;        // 当前的向前看符号

    public Parser(Lexer input) {
        this.input = input;
        this.lookahead = input.nextToken();
    }

    /**
     * 如果向前看词法类型能匹配x，那么就忽略并返回；
     * 否则，报错
     * @param x
     */
    public void match(int x) {
        if (lookahead.type == x) {
            consume();
        } else {
            throw new Error("expecting " + input.getTokenName(x) + "; found " + lookahead);
        }
    }


    public void consume() {
        lookahead = input.nextToken();
    }
}
